package com.sgb.goods.widget.pop;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.contract.api.AbstractCallBack;
import com.sgb.contract.api.ContractNet;
import com.sgb.contract.utils.ArouterConfigs;
import com.sgb.contract.view.widget.TagBuilder;
import com.sgb.goods.R;
import com.sgb.goods.entity.ContractInfoEntity;
import com.sgb.goods.entity.order.OrderPayDetailEntity;
import com.sgb.goods.widget.MaxHeightRecyclerView;
import com.sgb.goods.widget.pop.adapter.ContractInfoDetailAdapter;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.DisplayUtil;
import com.swgk.core.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 合同信息弹窗
 * @author cyj
 */
public class ContractInfoPop extends BasePopWindow<OrderPayDetailEntity> {

    private TextView business_name, order_num, type;

    public ContractInfoPop(Context context) {
        super(context, DIR_DOWN_UP);
    }

    private List<ContractInfoEntity.Records> data = new ArrayList<>();
    private ContractInfoDetailAdapter adapter;

    @Override
    protected int popLayout() {
        return R.layout.goods_pop_pay_info;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        TextView ivClose = holder.getPop().findViewById(R.id.close);
        business_name = holder.getPop().findViewById(R.id.business_name);
        order_num = holder.getPop().findViewById(R.id.order_num);
        type = holder.getPop().findViewById(R.id.type);
        TextView title = holder.getPop().findViewById(R.id.title);
        title.setText("合同信息");
        MaxHeightRecyclerView maxHeightRecyclerView = holder.getPop().findViewById(R.id.recyclerView);
        ivClose.setOnClickListener(v -> this.dismiss());
        adapter = new ContractInfoDetailAdapter(mContext, null);
        maxHeightRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        maxHeightRecyclerView.setAdapter(adapter);
        adapter.setOnItemClick(new AdapterOnItemClick<ContractInfoEntity.Records>() {
            @Override
            public void onItemClick(ContractInfoEntity.Records entity, int position) {
                checkStatusByNowStatus(entity);
            }
        });
    }

    public void showPop(View view, ContractInfoEntity entity, String shopType) {
        if(null == entity.records || entity.records.size() <= 0) {
            // 双方均未创建合同时不展示
            return;
        }
        this.data = entity.records;
        business_name.setText(entity.companyName);
        order_num.setText(entity.orderId);
        if ("1".equals(shopType)){
            type.setText("商家");
        }else {
            type.setText("采购商");
        }
        adapter.setDatas(data);
        setHeight(DisplayUtil.dip2px(mContext, 450));
        this.showDownPopwindow(view, true);
    }

    /**
     * 跳转订单详情界面
     */
    private void startOrderDetails(ContractInfoEntity.Records record) {
        boolean isShowBtn = false;
        if ("0".equals(record.isTaker)) {
            // 参与方 拟定中  已作废(非参与方已签署)
            if ("1".equals(record.status)) {
                return;
            } else if ("5".equals(record.status) && 0 == record.signStatus) {
                return;
            }
            if ("7".equals(record.status)) {
                isShowBtn = true;
            }
        } else {
            if ("7".equals(record.status)) {
                isShowBtn = true;
            } else if("1".equals(record.status) && StringUtil.isEmpty(record.localUrl)) {
                RoundDialog.Builder builder = new TagBuilder(mContext)
                        .setTag("checkStatusByNowStatus")
                        .title("温馨提示")
                        .content("当前合同暂无内容\n请在电脑端编辑后再操作！")
                        .contentGravity(Gravity.CENTER)
                        .cancelable(false)
                        .confirmCallback(v -> dismiss());
                builder.singleBtn(true);
                builder.confirm("知道了");
                builder.show();
                return;
            }
        }

        // 查看合同
        ARouter.getInstance().build(ArouterConfigs.AR_SIGN_CONTRACT)
                .withBoolean("isShowOperationLayout", isShowBtn)
                .withString("contractId", record.id)
                .navigation();

        dismiss();
    }

    /**
     * 检查合同状态
     *
     * @param record
     */
    private void checkStatusByNowStatus(ContractInfoEntity.Records record) {
        // 兼容销售订单列表
        if("8".equals(record.status)) {
            return;
        }
        //PC端的接口没有用6，7这两个类型，把6，7统一改成2，
        ContractNet.getInstance().getContractApi().checkStatusByNowStatus(record.no, String.valueOf("6".equals(record.status) || "7".equals(record.status) ? "2" : record.status), record.appSignStatus,
                record.appSignTag, record.appAuditStatus).enqueue(new AbstractCallBack<BaseEntity<Boolean>>() {
                    @Override
                    public void onSuccess(BaseEntity<Boolean> data) {
                        // 查看合同
                        startOrderDetails(record);
                    }

                    @Override
                    public void onBusinessFailure(BaseEntity entity) {
                        RoundDialog.Builder builder = new TagBuilder(mContext)
                                .setTag("checkStatusByNowStatus")
                                .title("温馨提示")
                                .content(entity.getMsg())
                                .contentGravity(Gravity.CENTER)
                                .cancelable(false)
                                .confirmCallback(v -> dismiss());
                        builder.singleBtn(true);
                        builder.confirm("知道了");
                        builder.show();
                    }
                });
    }
}
